import { User, UserManager } from "oidc-client";

// { "code", "authorization_code" },
// { "token", "implicit" },
// { "id_token", "implicit" },
// { "id_token token", "implicit" },
// { "code id_token", "hybrid" },
// { "code token", "hybrid" },
// { "code id_token token", "hybrid" }

class AuthManager extends UserManager {
    constructor() {
        super({
            authority: import.meta.env.VITE_AUTHORITY,
            client_id: import.meta.env.VITE_CLIENT_ID,
            client_secret: import.meta.env.VITE_CLIENT_SECRET,
            response_type: import.meta.env.VITE_RESPONSE_TYPE,
            scope: import.meta.env.VITE_SCOPE,
            redirect_uri: import.meta.env.VITE_REDIRECT_URI,
            post_logout_redirect_uri: import.meta.env.VITE_POST_LOGOUT_REDIRECT_URI
        })
    }
    getToken() {
        let key = `oidc.user:${this.settings.authority}:${this.settings.client_id}`;
        let str = sessionStorage.getItem(key)
        if (str == null) {
            return ''
        }
        let user = JSON.parse(str) as User
        return `${user?.token_type} ${user?.access_token}`
    }
}

const authManager = new AuthManager()
export default authManager